import 'dart:convert';
import 'package:cloud_medicine/components/util.dart';
import 'package:cloud_medicine/http/HttpUtil.dart';
import 'package:cloud_medicine/http/config.dart';
import 'package:dio/dio.dart';

// 获取帖子列表接口
Future getPostList_api(String url, dynamic data) async {
  Response res = await HttpUtil.get(
    url,
    parameters: data,
  );
  return jsonDecode(res.data);
}

// 获取帖子详情接口
Future getPost_api(String url, dynamic data) async {
  String token = await getToken();
  Response res = await HttpUtil.get(
    url,
    parameters: data,
    headers: {'Authorization': token},
  );
  return jsonDecode(res.data);
}

// 点赞/取消点赞
Future alterLikeStatus_api(String url, dynamic data) async {
  String token = await getToken();
  await HttpUtil.request(
    url,
    method: HttpUtil.PUT,
    parameters: data,
    headers: {'Authorization': token},
    onError: (error) {
      errorToast(error);
    },
  );
}

// 修改帖子
Future alterPost_api(String url, dynamic data) async {
  String token = await getToken();
  Response res = await HttpUtil.request(
    url,
    method: HttpUtil.PUT,
    parameters: data,
    headers: {'Authorization': token},
  );
  return jsonDecode(res.data);
}

// TODO: 删除帖子
Future deletePost_api(String url, dynamic data) async {
  String token = await getToken();
  Response res = await HttpUtil.request(
    '$backIP/post/deletePost',
    method: HttpUtil.DELETE,
    parameters: data,
    headers: {'Authorization': token},
  );
  return jsonDecode(res.data);
}

// 增加帖子
Future addPost_api(String url, dynamic data) async {
  String token = await getToken();
  Response res = await HttpUtil.request(
    url,
    method: HttpUtil.POST,
    parameters: data,
    headers: {'Authorization': token},
  );
  return jsonDecode(res.data);
}

// 集成搜索
Future search_api(dynamic data) async {
  String token = await getToken();
  Response res = await HttpUtil.get(
    '$backIP/search',
    parameters: data,
    headers: {'Authorization': token},
  );
  return jsonDecode(res.data);
}